﻿using Lcygcc.Domain.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lcygcc.Domain.Repository.Entity;
using Lcygcc.Infrastructure.Repository;
using Lcygcc.Infrastructure.Logs;

namespace Lcygcc.Domain.ServiceImpl
{
    public class SyncService : ISyncService
    {
        private readonly IBaseRepository mBaseRepository;


        public SyncService(IBaseRepository baseRepository)
        {
            mBaseRepository = baseRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <param name="serviceType"></param>
        /// <param name="dataType"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<SyncTemp> GetTempData(int status, int serviceType, int dataType, int count)
        {
            // var list = mBaseRepository.db.Queryable<DicType>().Where(it => it.Valid == 1).ToList();

            var list = mBaseRepository.db.Queryable<SyncTemp>().Where(it => it.SendNum <= 5).ToList();
            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="syncTempList"></param>
        public void MoveTempToHistoryData(List<SyncTemp> syncTempList)
        {
            if (syncTempList.Count <= 0)
            {
                return;
            }
            List<SyncHistory> historyList = Convert(syncTempList);
            int count = mBaseRepository.db.Insertable<SyncHistory>(historyList).ExecuteCommand();
            if (count > 0)
            {
                mBaseRepository.db.Deleteable<SyncTemp>(syncTempList).ExecuteCommand();
            }
        }
        /// <summary>
        /// /
        /// </summary>
        /// <param name="stncTempList"></param>
        public void UpdateTempFail(List<SyncTemp> syncTempList)
        {
            try
            {
                mBaseRepository.db.Updateable<SyncTemp>(syncTempList).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Logger.Error("-----SyncService.UpdateTempFail-----", ex);
            }
        }

        private List<SyncHistory> Convert(List<SyncTemp> syncTempList)
        {
            List<SyncHistory> historyList = new List<SyncHistory>();
            foreach (var temp in syncTempList)
            {
                var history = new SyncHistory();
                history.AddTime = temp.AddTime;
                history.BusinessId = temp.BusinessId;
                history.DataType = temp.DataType;
                history.FailMessage = temp.FailMessage;
                history.RequestType = temp.RequestType;
                history.ProtocolData = temp.ProtocolData;
                history.Remark = temp.Remark;
                history.SendNum = temp.SendNum;
                history.SeqId = temp.SeqId;
                history.ServiceId = temp.ServiceId;
                history.ServiceType = temp.ServiceType;
                history.Status = temp.Status;
                history.UpdateTime = DateTime.Now;
                historyList.Add(history);
            }
            return historyList;
        }
    }
}
